package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.VO.BlogVO;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageResult<BlogVO> selectByPage(BlogQuery blogQuery) {
        //int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        //List<BlogVO> list = blogMapper.selectByPage(blogQuery.getLimit(), offset);
        //int totalCount = blogMapper.selectTotalCount(blogMapper);
        //return new PageResult<>(0, "", totalCount, list);
        PageHelper.startPage(blogQuery.getPage(), blogQuery.getLimit());
        List<BlogVO> list = blogMapper.selectByPage(blogQuery);
        PageInfo<BlogVO> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public void add(Blog blog) {
        blogMapper.add(blog);
    }
}
